package org.flexthinker.binding.ui.form.binding.support
{
    import org.flexthinker.binding.bean.Binding;
    import org.flexthinker.binding.form.support.DefaultValidatingFormModel;
    import org.flexthinker.binding.value.support.MockBean;
    import org.hamcrest.assertThat;
    import org.hamcrest.object.equalTo;

    public class FieldBinderTest
    {
        private var label:MockLabel;
        private var bean:MockBean;

        public function FieldBinderTest()
        {
        }


        [Before]
        public function setUp():void
        {
            label = new MockLabel();
            bean = new MockBean();
        }

        [Test]
        public function testBind():void
        {
            var fm:DefaultValidatingFormModel = new DefaultValidatingFormModel(bean);
            var fb:FieldBinder = new FieldBinder(label, "simpleString");
            assertThat(label.text, equalTo("no_text"));

            var b:Binding = fb.getBinding(fm);
            b.bind();

            assertThat(label.text, equalTo(null));
            assertThat("value should have been set only once", label.textSetCounter, equalTo(1));

            bean.simpleString = "new string";
            assertThat(label.text, equalTo("new string"));
            assertThat("set text() should have been called only once",label.textSetCounter, equalTo(2));

            label.text = "no text";

            assertThat("Changing label's text shouldn't change the bean", bean.simpleString, equalTo("new string"));

            bean.simpleString = "another string";
            assertThat(label.text, equalTo("another string"));

            b.unbind();
            bean.simpleString = "final string";
            assertThat("binding should have been stopped after calling unbind", label.text, equalTo("another string"));
        }

        [Test]
        public function testBindWhenFormModelChanges():void
        {
            var fm:DefaultValidatingFormModel = new DefaultValidatingFormModel(bean);
            var fb:FieldBinder = new FieldBinder(label, "simpleString");
            assertThat(label.text, equalTo("no_text"));

            var b:Binding = fb.getBinding(fm);
            b.bind();

            bean.simpleString = "AAA";
            assertThat(label.text,  equalTo("AAA"));

            var newBean:MockBean = new MockBean();
            newBean.simpleString = "BBB";
            fm.setFormObject( newBean );

            assertThat("form model changes should be captured by Binding", label.text,  equalTo("BBB"));
        }

    }
}

import mx.core.UIComponent;

class MockLabel extends UIComponent
{
    private var _text:String = "no_text";

    public var textSetCounter:int = 0;

    public var textGetCounter:int = 0;

    public function MockLabel():void
    {

    }

    public function get text():String
    {
        textGetCounter++;
        return _text;
    }

    public function set text(value:String):void
    {
        _text = value;
        textSetCounter++;
    }
}
